import axios from 'axios'
import React, { useState, useEffect } from 'react'
import { Table, Button, Tag, notification } from 'antd'
import NewsCategory from '../news-manage/NewsCategory'
export default function AuditList(props) {
    const [dataSource, setdataSource] = useState([])
    const { username } = JSON.parse(localStorage.getItem('token'))
    useEffect(() => {
        query()
    }, [])

    const query = () => {
        axios.get(`/news?author=${username}&auditState_ne=0&publishState_lte=1&_expand=category`).then(res => {
            setdataSource(res.data)
        })
    }
    const handelAudit = () => { }
    const columns = [
        {
            title: '新闻标题',
            dataIndex: 'title',
            render: (title, item) => {
                return <a href={`#/news-manage/preview/${item.id}`}>{title}</a>
            }
        },
        {
            title: '作者',
            dataIndex: 'author',

        },
        {
            title: '新闻分类',
            dataIndex: 'category',
            render: (category) => <div>{category.title}</div>

        },
        {
            title: '审核状态',
            dataIndex: 'auditState',
            render: (auditState) => {
                const colorList = ['', 'orange', 'green', 'red']
                const curList = ['', '待审核', '审核成功', '审核失败']
                return < Tag color={colorList[auditState]}>  {curList[auditState]}</Tag>
            }
        },
        {
            title: '操作',
            render: (item) => {
                return <div>
                    {
                        item.auditState === 1 && <Button danger onClick={() => handleBtn(item, 'remove')}>
                            撤销
                        </Button>
                    }
                    {
                        item.auditState === 2 && <Button style={{ color: 'green' }} onClick={() => handleBtn(item, 'setup')}>
                            发布
                        </Button>
                    }
                    {
                        item.auditState === 3 && <Button type='primary' onClick={() => handleBtn(item, 'editor')}>
                            修改
                        </Button>
                    }
                </div >
            }
        },
    ]

    const handleBtn = (item, type) => {
        switch (type) {
            case 'remove':
                axios.patch(`/news/${item.id}`, { auditState: 0 }).then(res => {
                    query()
                    notification.info({
                        message: '通知',
                        description: `您可以通过草稿箱中查看你的新闻`,
                        placement: 'bottomRight'
                    })
                })
                break;
            case 'setup':
                axios.patch(`/news/${item.id}`, { pubilshState: 2, publishTime: Date.now() }).then(res => {
                    query()
                    notification.info({
                        message: '通知',
                        description: `您可以通过发布管理-发布中查看你的新闻`,
                        placement: 'bottomRight'
                    })
                    props.history.push('/publish-manage/published')
                })
                // props.history.push(`/news-mange-update/${item.id}`)
                break;
            case 'editor':
                props.history.push(`/news-manage/update/${item.id}`)
                break;
            default:
                break;
        }
    }
    return (
        <div>
            <Table dataSource={dataSource} columns={columns} pagination={{
                pageSize: 5
            }} rowKey={(item) => item.id} />
        </div>
    )
}
